package com.xebest.web.controller.purch;  

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.XeVenExcelUtil;
import com.xebest.web.controller.export.XeVenPurchExportDTO;
import com.xebest.web.form.XeVenPurchForm;
import com.xebest.web.service.XeVenPurchCenterExportService;
import com.xebest.web.view.ErpUserInfo;

/**
 * 采购中心导出
 * @author 赵盼飞
 */
@RestController
@RequestMapping("purchCenterExport")
public class XeVenPurchCenterExportController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(XeVenPurchCenterExportController.class);//日志记录类
	
	@Autowired
	private XeVenPurchCenterExportService purchCenterExportService;//采购中心
	
	@Value("${xeven.erp.exportTempletDir}")
    private String exportTempletDir;//导出文件目录
  
	/**
     * 导出采购单列表
	 * @author 赵盼飞
     */
	@RequestMapping("purchList")
    public @ResponseBody BaseResponse queryPurchExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		
		try{
			//测试
			String[] str = new String[] { "purchOrder", "orderId","supplierName", "storeName", "frontMoney", "purchAmount",
					"settlementMethod", "checkStatus", "purchStatus", "addType", "purchType", "signDate", "expectedArrivalDate",
					"completeDate" };
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
			form.setUserCode(userInfo.getUserCode());
//			form.setUserCode("SH171129731505");
//			form.setPurchOrder("CG17121959741440");
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchExportList(form);
			
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单列表接口异常：{}" , e);
			return sendAjaxError();
		}
    }

	/**
	 * 导出采购单导出详情列表
	 * @author 赵盼飞
	 */
	@RequestMapping("purchDetailList")
	public @ResponseBody BaseResponse queryPurchDetailExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		
		try{
			String[] str = new String[] { "purchOrder","orderId", "supplierName", "storeName", "frontMoney", "purchAmount",
					"settlementMethod", "checkStatus", "purchStatus", "addType", "purchType", "signDate", "expectedArrivalDate",
					"completeDate", "supplierMobile", "supplierPhone", "createUser", "proSku", "proNo", "proName",
					"categoryName", "priceType", "proUnit", "specValue", "buyPrice", "proNum", "currentInNum",
					"proAmount", "receivedCharges" };
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
			form.setUserCode(userInfo.getUserCode());
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchDetailExportList(form);
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHDETAILLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单导出详情接口异常：{}" , e);
			return sendAjaxError();
		}
	}

	/**
	 * 导出采购审核列表
	 * @author 李书峰
	 */
	@RequestMapping("purchCheckList")
    public @ResponseBody BaseResponse queryPurchCheckExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		form.setIsCheckList(1);
		try{
			//测试
			String[] str = new String[] {
					"purchOrder",
			        "supplierName",
			        "storeName",
			        "frontMoney",
			        "purchAmount",
			        "settlementMethod",
			        "checkStatus",
			        "purchStatus",
			        "addType",
			        "purchType",
			        "signDate",
			        "expectedArrivalDate",
			        "completeDate" 
			};
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
//			form.setUserCode("SH1709228481165");
			form.setUserCode(userInfo.getUserCode());
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchExportList(form);
			
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单列表接口异常：{}" , e);
			return sendAjaxError();
		}
    }

	/**
	 * 导出采购审核详细列表
	 * @author 李书峰
	 */
	@RequestMapping("purchDetailCheckList")
	public @ResponseBody BaseResponse queryPurchDetailCheckExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		form.setIsCheckList(1);
		try{
			String[] str = new String[] { "purchOrder","orderId", "supplierName", "storeName", "frontMoney", "purchAmount",
					"settlementMethod", "checkStatus", "purchStatus", "addType", "purchType", "signDate", "expectedArrivalDate",
					"completeDate", "supplierMobile", "supplierPhone", "createUser", "proSku", "proNo", "proName",
					"categoryName", "priceType", "proUnit", "specValue", "buyPrice", "proNum", "currentInNum",
					"proAmount", "receivedCharges" };
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
			form.setUserCode(userInfo.getUserCode());
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchDetailExportList(form);
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHDETAILLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单导出详情接口异常：{}" , e);
			return sendAjaxError();
		}
	}
	
	/**
	 * 导出鲜咨达审核列表
	 * @author 李书峰
	 */
	@RequestMapping("purchXianList")
    public @ResponseBody BaseResponse queryPurchXianExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		form.setIsCheckList(2);
		try{
			//测试
			String[] str = new String[] {
					"purchOrder",
			        "supplierName",
			        "storeName",
			        "frontMoney",
			        "purchAmount",
			        "settlementMethod",
			        "checkStatus",
			        "purchStatus",
			        "addType",
			        "purchType",
			        "signDate",
			        "expectedArrivalDate",
			        "completeDate" 
			};
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
			form.setUserCode(userInfo.getUserCode());
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchExportList(form);
			
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单列表接口异常：{}" , e);
			return sendAjaxError();
		}
    }

	/**
	 * 导出鲜咨达审核详细列表
	 * @author 李书峰
	 */
	@RequestMapping("purchDetailXianList")
	public @ResponseBody BaseResponse queryPurchDetailXianExportList(@ModelAttribute XeVenPurchForm form,HttpServletResponse response){
		form.setIsCheckList(2);
		try{
			String[] str = new String[] { "purchOrder","orderId", "supplierName", "storeName", "frontMoney", "purchAmount",
					"settlementMethod", "checkStatus", "purchStatus", "addType", "purchType", "signDate", "expectedArrivalDate",
					"completeDate", "supplierMobile", "supplierPhone", "createUser", "proSku", "proNo", "proName",
					"categoryName", "priceType", "proUnit", "specValue", "buyPrice", "proNum", "currentInNum",
					"proAmount", "receivedCharges" };
			form.setFieldArr(str);
			//1、验证参数
			if (form.getFieldArr().length<=0) {
				return sendAjaxError(BASEMessage.PARAM_ERROR);
			}
			ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
			form.setUserCode(userInfo.getUserCode());
			//2.获取数据
			List<XeVenPurchExportDTO> purchExportList = purchCenterExportService.queryPurchDetailExportList(form);
			if(null != purchExportList && purchExportList.size()>0){
				//3.导出
				boolean rs = XeVenExcelUtil.exportExcelService(purchExportList,new XeVenPurchExportDTO(),form.getFieldArr(),exportTempletDir,"PURCHDETAILLIST-",response);
				if (rs) {
					return sendAjaxOK();
				}else{
					return sendAjaxError();
				}
			}else{
				return sendAjaxOK("导出数据为空！");
			}
		}catch (Exception e) {
			log.error("导出采购单导出详情接口异常：{}" , e);
			return sendAjaxError();
		}
	}
	
}
  
